package com.dhcc.sdc.cdss.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.dhcc.sdc.base.dao.SdcBaseAssessmentMapper;
import com.dhcc.sdc.base.entity.SdcBaseAssessmentEntity;
import com.dhcc.sdc.cdss.dao.SdcCdssEvalMapper;
import com.dhcc.sdc.cdss.dto.SdcBaseAssessmentDto;
import com.dhcc.sdc.cdss.entity.SdcCdssEvalEntity;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sdc.cdss.dao.SdcCdssEvalMatchMapper;
import com.dhcc.sdc.cdss.service.ISdcCdssEvalMatchService;
import com.dhcc.sdc.cdss.entity.SdcCdssEvalMatchEntity;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 表数据服务层接口
 *
 * @ClassName: SdcCdssEvalMatchServiceImpl
 * @Description: TODO
 * @author zhangdc
 * @date 2022-07-25 14:03:52
 */
@Service("sdcCdssEvalMatchService")
public class SdcCdssEvalMatchServiceImpl extends BaseServiceImpl<SdcCdssEvalMatchMapper, SdcCdssEvalMatchEntity> implements ISdcCdssEvalMatchService{
    
    @Resource
    private SdcCdssEvalMapper cdssEvalMapper;
    @Resource
    private SdcBaseAssessmentMapper sdcBaseAssessmentMapper;

    @Override
    public List<SdcCdssEvalMatchEntity> page(Page<SdcCdssEvalMatchEntity> page, Map<String, Object> map) {
        return this.baseMapper.page(page, map);
    }
    @Override
    public List<SdcCdssEvalMatchEntity> list(Map<String, Object> map) {
        return this.baseMapper.list(map);
    }

    @Override
    public List<SdcBaseAssessmentDto> pageMatchAssessment(Page<SdcCdssEvalMatchEntity> page, Map<String, Object> params) {
        List<SdcBaseAssessmentDto> baseAssessmentDtos = CollUtil.newArrayList();
        String id = (String) params.get("id");
        String orderKey = "";
        if (!StringUtils.isEmpty(params.get("orderKey"))){
            orderKey = (String) params.get("orderKey");
        }

        SdcCdssEvalEntity cdssEvalEntity = cdssEvalMapper.selectById(Long.parseLong(id));
        if (!StringUtils.isEmpty(cdssEvalEntity)){
            params.put("itemId", cdssEvalEntity.getItemId());
        }else {
            return null;
        }

        //默认查询已对照的
        List<SdcCdssEvalMatchEntity> page1 = this.baseMapper.page(page, params);
        if (null != page1 && page1.size() > 0){
            for (SdcCdssEvalMatchEntity cdssEvalMatchEntity : page1){
                SdcBaseAssessmentDto baseAssessmentDto = new SdcBaseAssessmentDto();
                SdcBaseAssessmentEntity sdcBaseAssessmentEntity = sdcBaseAssessmentMapper.selectById(cdssEvalMatchEntity.getAssessmentId());
                if (!StringUtils.isEmpty(orderKey) && !sdcBaseAssessmentEntity.getDescription().contains(orderKey)){
                    continue;
                }
                BeanUtil.copyProperties(sdcBaseAssessmentEntity,baseAssessmentDto);
                baseAssessmentDto.setChecked(1);
                baseAssessmentDto.setIsDefault(cdssEvalMatchEntity.getIsDefault());
                baseAssessmentDtos.add(baseAssessmentDto);
            }
        }

        return baseAssessmentDtos;
    }

    @Override
    public List<SdcBaseAssessmentDto> pageAssessment(Page<SdcBaseAssessmentEntity> page, Map<String, Object> params) {
        List<SdcBaseAssessmentDto> res = CollUtil.newArrayList();
        String compare = (String) params.get("compare");
        String id = (String) params.get("id");
        String orderKey = "";
        if (!StringUtils.isEmpty(params.get("orderKey"))){
            orderKey = (String) params.get("orderKey");
        }

        params.put("description", orderKey);
        SdcCdssEvalEntity cdssEvalEntity = cdssEvalMapper.selectById(Long.parseLong(id));
        if (!StringUtils.isEmpty(cdssEvalEntity)){
            params.put("itemId", cdssEvalEntity.getItemId());
        }else {
            return null;
        }

        if (Objects.equals("all", compare)){
            //查询所有
            List<SdcCdssEvalMatchEntity> list = this.baseMapper.list(params);
            List<SdcBaseAssessmentEntity> page1 = sdcBaseAssessmentMapper.page(page, params);
            for (SdcBaseAssessmentEntity sdcBaseAssessmentEntity : page1){
                SdcBaseAssessmentDto sdcBaseAssessmentDto = new SdcBaseAssessmentDto();
                BeanUtil.copyProperties(sdcBaseAssessmentEntity,sdcBaseAssessmentDto);
                res.add(sdcBaseAssessmentDto);
            }
            if (list.size() == 0){
                return res;
            }else {
                for (SdcBaseAssessmentDto sdcBaseAssessmentDto : res){
                    for (SdcCdssEvalMatchEntity sdcCdssEvalMatchEntity : list){
                        if (Objects.equals(sdcBaseAssessmentDto.getId(), sdcCdssEvalMatchEntity.getAssessmentId())){
                            sdcBaseAssessmentDto.setChecked(1);
                            sdcBaseAssessmentDto.setIsDefault(sdcCdssEvalMatchEntity.getIsDefault());
                        }
                    }
                }
                return res;
            }
        }else if (Objects.equals("noMatch", compare)){
            //查询未对照的
            List<Long> matchAssessmentIds = this.baseMapper.selectMatchAssessmentIds(params);
            List<SdcBaseAssessmentEntity> sdcBaseAssessmentEntities = sdcBaseAssessmentMapper.pageExcludeMatch(page, params, matchAssessmentIds);
            for (SdcBaseAssessmentEntity baseAssessmentEntity : sdcBaseAssessmentEntities){
                SdcBaseAssessmentDto sdcBaseAssessmentDto = new SdcBaseAssessmentDto();
                BeanUtil.copyProperties(baseAssessmentEntity,sdcBaseAssessmentDto);
                res.add(sdcBaseAssessmentDto);
            }
        }

        return res;
    }
}
